package com.example.demo.common.utils.demo;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.utils.BaseResponse;
import com.example.demo.common.utils.HttpUtil;
import com.example.demo.common.utils.RSAHelper;
import com.example.demo.common.utils.demo.common.Config;
import com.example.demo.common.utils.demo.model.BillItemCallBackRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhangws email:1127145966@qq.com
 * @Description: 代付接口调用demo
 * @Date:2019/7/22 0022 10:03
 */
public class Substitution {
    static Logger log = LoggerFactory.getLogger(HttpUtil.class);
    /**
     * 提交订单
     */
    public static void submitBill(String payeeName,
                                  String payeeAccount,String payeeIdCard,
                                  String amount,String thirdOrderId,
                                  String payeePhone,String payeeBankName,String payeeBankNo,String thirdBizOrderId,String payType){
        String url = Config.baseUrl+"/open/api/v1/bill/submitBill";
        String paramsJson = "{\"payeeName\":\""+payeeName+"\",\"payeeAccount\":\""+payeeAccount+"\",\"payeeIdCard\":\""+payeeIdCard+"\",\"amount\":\""+amount+"\"," +
                "\"thirdOrderId\":\""+thirdOrderId+"\",\"payeePhone\":\""+payeePhone+"\",\"payeeBankName\":\""+payeeBankName+"\",\"payeeBankNo\":\""+payeeBankNo+"\",\"thirdBizOrderId\":\""+thirdBizOrderId+"\",\"payType\":\""+payType+"\"}";
        //公钥加密
        BaseResponse baseResponse = HttpUtil.post(url,paramsJson,Config.appKey,Config.secretkey,Config.publicKey);
        if (baseResponse.isSuccess()){
            String rs = baseResponse.getData().toString();
            //公钥解密
            if (StringUtils.isNotBlank(rs)){
                try {
                    String dec = RSAHelper.decryptByPublicKey(rs,Config.publicKey);
                    log.info("解密结果:{}",dec);
                }catch (Exception e){
                    log.error("publicKey 有误");

                }
            }
        }
    }

    /**
     * 确认发放
     * billId 蓝犀订单ID 由上一个接口返回
     */
    public static void sureGrant(String billId,String mainstayId,String invoiceCode){
        String url = Config.baseUrl+"/open/api/v1/bill/sureGrant";
        String paramsJson = "{\"billId\":\""+billId+"\",\"mainstayId\":\""+mainstayId+"\",\"invoiceCode\":\""+invoiceCode+"\"}";
        //公钥加密
        BaseResponse baseResponse = HttpUtil.post(url,paramsJson,Config.appKey,Config.secretkey,Config.publicKey);
        log.info(String.format("发放结果:%s",JSONObject.toJSONString(baseResponse)));
    }


    /**
     * 订单取消
     * billId 蓝犀订单ID
     */
    public static void cancel(String billId,String reason){
        String url = Config.baseUrl+"/open/api/v1/bill/cancel";
        String paramsJson = "{\"billId\":\""+billId+"\",\"reason\":\""+reason+"\"}";
        //公钥加密
        BaseResponse baseResponse = HttpUtil.post(url,paramsJson,Config.appKey,Config.secretkey,Config.publicKey);
        log.info(String.format("取消结果:%s", JSONObject.toJSONString(baseResponse)));
    }
    /**
     * 支付成功回调
     */
    public static void payCallBack(BillItemCallBackRequest billItemCallBackRequest){
        String url = Config.baseUrl+"/open/api/v1/bill/payCallBack";
        String paramsJson = JSONObject.toJSONString(billItemCallBackRequest);
        BaseResponse baseResponse = HttpUtil.post(url,paramsJson,Config.appKey,Config.secretkey,Config.publicKey);
        log.info(String.format("回调结果:%s", JSONObject.toJSONString(baseResponse)));
    }

    /**
     * 支付宝定制提交
     * @param payeeName
     * @param payeeAccount
     * @param payeeIdCard
     * @param amount
     * @param thirdOrderId
     * @param payeePhone
     * @param payeeBankName
     * @param payeeBankNo
     * @param thirdBizOrderId
     * @param invoiceCode
     * @param mainstayId
     */
    public static void zfbSubmitBill(String payeeName,
                                     String payeeAccount,String payeeIdCard,
                                     String amount,String thirdOrderId,
                                     String payeePhone,String payeeBankName,String payeeBankNo,String thirdBizOrderId,String invoiceCode,String mainstayId){
        String url = Config.baseUrl+"/open/api/v1/bill/zfbCommission/submitBill";
        String paramsJson = "{\"payeeName\":\""+payeeName+"\",\"payeeAccount\":\""+payeeAccount+"\",\"payeeIdCard\":\""+payeeIdCard+"\",\"amount\":\""+amount+"\"," +
                "\"thirdOrderId\":\""+thirdOrderId+"\",\"payeePhone\":\""+payeePhone+"\",\"payeeBankName\":\""+payeeBankName+"\",\"payeeBankNo\":\""+payeeBankNo+"\",\"thirdBizOrderId\":\""+thirdBizOrderId+"\",\"invoiceCode\":\""+invoiceCode+"\",\"mainstayId\":\""+mainstayId+"\"}";
        //公钥加密
        BaseResponse baseResponse = HttpUtil.post(url,paramsJson,Config.appKey,Config.secretkey,Config.publicKey);
        if (baseResponse.isSuccess()){
            String rs = baseResponse.getData().toString();
            //公钥解密
            if (StringUtils.isNotBlank(rs)){
                try {
                    String dec = RSAHelper.decryptByPublicKey(rs,Config.publicKey);
                    log.info("解密结果:{}",dec);
                }catch (Exception e){
                    log.error("publicKey 有误");

                }
            }
        }
    }
}
